  function [fidmat,dimat]=saveiof( fidmat , act,  opath, names , dimat, varargin );
% function [fidmat,dimat]=saveiof( fidmat , act,  opath, names , dimat, varargin );
%
% Save through IO Files 
% 
% Code should be used in loops to store matrices in blocks 
% Inputs
% ------
% fidmat   Index of fids for the files to save 
% act      = 0   Open files for writing ( fidmat=[] ) 
%          = 1   Write Inputs to File 
%          = 2   Close File 
% opath    Path where to store the files 
% names    Cell with the names for the files 
% dimat    Dimension Matrix  Records column dimension of the 
%          Matrices to store ( set=[] for act 0 ) 
% varargin  Inputs to be stored 
% 
% Note: Matrices to be stored should ne given as 
%       [ndr x (m x n) ] 
%       Columns and Pages stored together to retrieve 
%       Output in the appropriate order 
% 
% When the final data file will be retrieved the dimension will be 
% given by a dimat file also which allows for reshaping the output  
% 
% Alejandro Justiniano 3/31/04
% 
% 
narg=nargin - 5;
[ns,nc]=size(names);
cuc=cd; 
%Check an action will be performed 
%---------------------------------
ch=(0:2)'; 
if isempty( find( ch == act ) ) == 1 
    error(' act must be between 0 and 1 ');  
end; 
clear ch; 
% Check input dimension 
%----------------------
if isempty(names) == 1 
    error('Need provide Names Cell'); 
end; 
if (nc > 1) 
    if ns > 1; 
        error('Names cell must be a column array '); 
    elseif ns == 1; 
        names=r2c(names); 
        ns=nc; 
    end; 
    clear nc; 
end; 
if act == 1 
    if ns < 1 
        error('Need provide inputs to write'); 
    end
    if sum( any( fidmat == -1 ) )~=0; 
        error('Cannot write one of the files'); 
    end; 
end; 
ii=1; 
cd(opath); 
for ii=1:ns; 
    switch act 
    case 0 
        %=========================
        % Open files for writing
        %=========================
        if ii ==1 ;
            fidmat=zeros(ns,1);
        end; 
        fidmat(ii,1)= fopen( names{ii} , 'w' );
        if fidmat(ii,1) == -1 
            fpraid( ['Warning! File ', names{ii},' could not be opened for writing'] ); 
        end
        dimat=[]; 
    case 1 
        %=========================
        % Write data to the files 
        %=========================
        temp=varargin{ii};
        [ndr,ncol]=size(temp);
        if ( isempty(dimat) == 1 | dimat(ii) == 0 ); 
            if ii == 1 
                dimat=zeros(ns,1);
            end            
            dimat(ii) = ncol;  
        end;          
        % Key to saving the files is store per row 
        % such that can then recover the right dimension
        % Second entry of control stores the column dimension to 
        % reshape the matrices accordingly later 
        count=fwrite( fidmat(ii) ,temp','float64');
        if count ~= (ndr*ncol); 
            fpraid(['Warning! Some elements in File ', names{ii},' could not be written ']); 
        end; 
    case 2         
        %=========================
        %       Close files  
        %=========================
        ch=fclose( fidmat(ii) );
        if ch ~= 0; 
            fpraid(['Warning! File ', names{ii},' could not be closed ']); 
        end 
    end 
end 
cd(cuc); 